<?php
/**
 * QQAS - share your knowledge to the world!
 *
 * @author		xiaochong0302 <xiaochong0302@gmail.com>
 * @copyright	Copyright (c) 2011, Koogua Studio.
 * @license		http://www.qqas.org/user_guide/license.html
 * @link		http://www.qqas.org
 * @since		Version 1.0
 * @filesource
 */


// ------------------------------------------------------------------------

/**
 * FAQ
 *
 * @category	Admin
 */
class FaqAction extends AdminAction
{

	protected $Faq;


	//初始化
	public function _initialize()
	{
		parent::_initialize();
		$this->Faq = D('Faq');
	}


	//FAQ列表
	public function faqList()
	{
		import('ORG.Util.Page');

		$where = $order = array();
		$parameter = '';

		$total = $this->Faq->where($where)->count();
		$Page = new Page($total);
		$Page->parameter = $parameter;
		$pagination = $Page->show();
		$faqList = $this->Faq->where($where)->order($order)->limit($Page->firstRow.','.$Page->listRows)->select();

		$this->assign('pagination', $pagination);
		$this->assign('faqList', $faqList);
		$this->display();
	}


	//批量管理FAQ
	public function manageFaq()
	{
		if ($this->isPost())
		{
		    if (!empty($_POST['display_order']))
		    {
		    	foreach ($_POST['display_order'] as $key => $value)
		    	{
		    	    $map = array('faq_id' => $key);
		    	    $data = array('display_order' => $value);
		    		$this->Faq->where($map)->save($data);
		    	}
		    }
		    $map = array('faq_id' => array('in', $_POST['mods']));
			$this->Faq->where($map)->delete();

			$this->Faq->cacheFaqs();
			$this->success(L('faq.faq_updated_succeed'));
		}
	}


	//新增faq
	public function addFaq()
	{
		$this->editFaq();
	}


	//编辑faq
	public function editFaq()
	{
		$faqID = intval($_REQUEST['faq_id']);
		$faqInfo = $this->Faq->find($faqID);

		if ($this->isPost())
		{
		    if (!empty($_POST['identifier']))
		    {
		        if (!preg_match("/^[a-z0-9\-_]+$/i", $_POST['identifier']))
		        {
		            $this->error(L('faq.faq_identifier_invalid'));
		        }

		        $map = array(
		            'faq_id' => array('neq', $faqID),
		            'identifier' => $_POST['identifier'],
		        );
		    	$result = $this->Faq->where($map)->find();
		    	if (!empty($result))
		    	{
		    		$this->error(L('faq.faq_identifier_existed'));
		    	}
		    }

            if (empty($_POST['title']))
            {
            	$this->error(L('faq.faq_title_invalid'));
            }

			$data = array(
				'identifier'    => $_POST['identifier'],
				'title'         => $_POST['title'],
				'content'       => $_POST['content'],
				'edit_time'     => time(),
				'display_order' => intval($_POST['display_order']),
			);

			if (empty($faqInfo))
			{
			    $data['create_time'] = time();
				$this->Faq->add($data);
				$message = L('faq.faq_added_succeed');
			}
			else
			{
				$map = array('faq_id' => $faqID);
				$this->Faq->where($map)->save($data);
				$message = L('faq.faq_edited_succeed');
			}

			$this->Faq->cacheFaqs();

			$this->assign('jumpUrl', __URL__.'/faqList');
			$this->success($message);
		}
		else
		{
			$this->assign('rs', $faqInfo);
			$this->display('editFaq');
		}
	}

}
// End  Class


/* End of file FaqAction.class.php */
/* Location: ./App/Lib/Action/Admin/FaqAction.class.php */